Validation Rule

  • Rules
    #1) Before Or Equal (Date) – before_or_equal:date

    This validation rule only allows using a value preceding or equal to the given date.

    #2) Between – between:min,max

    This validation rule only allows using a size between the given min and max.

    #3) Date – date

    This validation rule only allows using a valid, non-relative date according to the strtotime PHP function.

    #4) Date Format – date_format:format

    Under this validation rule, the field must match the given format.

    #5) Different – different:field

    Under this validation rule, the field must have a different value than the field.

    #6) Distinct – distinct

    When working with arrays, under this validation rule, the field must not have any duplicate values.

    #7) Email – email

    Under this validation rule, the field must be formatted as an email address.

    #8) Image (File) – image

    Under this validation rule, the field must be an image (jpeg, png, bmp, gif, svg, or webp).

    #9) Nullable – nullable

    Under this validation rule, the field must be null.,

    #10) Numeric – numeric

    Under this validation rule, the field must be numeric.

    #11) Regular Expression – regex:pattern

    Under this validation rule, the field must match the given regular expression.

    #12) Required – required

    Under this validation rule, the field must be present in the input data and not empty.

    #13) Size – size:value

    Under this validation rule, the field must have a size matching the given value.

    #14) Sometimes – sometimes

    This validation rule runs validation checks against a field only if that field is present in the input array.

    #15) URL – url

    Under this validation rule, the field must be a valid URL.

  • Example

    1. controller

    
    
            namespace App\Http\Controllers;
            
            use Illuminate\Http\Request;
            use App\Student;
            
            class StudentController extends Controller
            {
            public function create()
            
            {
                    return view('create');
            }
            
            public function store(Request $request)
            
            {
                    $input = $request->all();
            
                    $request->validate([
                    'title' => 'required',
                    'name' => 'required|max:255',
                    'bday' => 'required|date',
                    'age' => 'required|numeric',
                    'gender' => 'required',
                    'phone' => 'required|min:10',
                    'address' => 'required|max:255',
                    'email' => 'required|email|max:255',
                    'password' => 'required|min:6|max:255',
                    't&c' => 'required',
                    ]);
            
                    $input['password'] = bcrypt($input['password']);
                    Student::create($input);
            
                    return back()->with('success','Successfully registered a new student!');
            
            }
            
            }
    
    

    2. in view form

    
    
       @if (count($errors) > 0)
            <div class="alert alert-danger">
                <ul>
                @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                        @endforeach
                </ul>
            </div>
            @endif
     
            @if ($message = Session::get('success'))
            <div class="alert alert-success alert-block">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
            </div>
            @endif